package game

import (
	"server/share/config"

	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/players"
)

func init() {
	players.SetMonitor(emitter)
}

// 接管事件触发 日常和成就系统
func emitter(u *updater.Updater, id int32, vs ...int32) {
	c := config.Data.Emitter[id]
	if c == nil {
		return
	}
	if c.Daily > 0 {
		eventUpdateDaily(u, c.Daily, vs[0], c.Replace)
	}
	if c.Record > 0 {
		eventUpdateRecord(u, c.Record, vs[0], c.Replace)
	}
}

func eventUpdateDaily(u *updater.Updater, k int32, v int32, replace int32) {
	switch replace {
	case 1:
		u.Set(k, v)
	case 2:
		if int64(v) > u.Val(k) {
			u.Set(k, v)
		}
	case 3:
		if int64(v) < u.Val(k) {
			u.Set(k, v)
		}
	default:
		u.Add(k, v)
	}
}

func eventUpdateRecord(u *updater.Updater, k int32, v int32, replace int32) {
	switch replace {
	case 1:
		u.Set(k, "val", v)
	case 2:
		if int64(v) > u.Val(k) {
			u.Set(k, "val", v)
		}
	case 3:
		if int64(v) < u.Val(k) {
			u.Set(k, "val", v)
		}
	default:
		u.Add(k, v)
	}
}
